package edzis.translatebuddy.view
{
	import edzis.translatebuddy.events.DataProxyEvent;
	import edzis.translatebuddy.model.AppProxy;
	import edzis.translatebuddy.model.DataProxy;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import edzis.translatebuddy.ApplicationFacade;
	
	import edzis.translatebuddy.view.components.ChoseSession;

	public class ChoseSessionMediator extends Mediator implements IMediator
	{
		
		public static const NAME:String = 'ChoseSessionMediator';
		
		private var view			: ChoseSession;
		private var _dataProxy		: DataProxy;
		private var _appProxy		: AppProxy;
		
		
		public function ChoseSessionMediator(viewComponent:Object = null) {
			super(NAME, viewComponent);
			view = viewComponent as ChoseSession;
			// Add Listeners
			view.addEventListener(DataProxyEvent.TRY_CREATE_SESSION, tryCreateSession);
			view.addEventListener(DataProxyEvent.SELECT_SESSION, selectSession);
		}
		
		override public function onRegister():void {
			super.onRegister();
			_dataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
			_appProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
			view.init(_dataProxy.allSessions);
		}
		
		
	/************************************
	/*	PRIVATE LISTENERS
	/***********************************/
		private function tryCreateSession(e:DataProxyEvent):void {
			ArthropodDebug.log("tryCreateSession");
			_dataProxy.tryCreateSession(e.data as String);
		}
		
		private function selectSession(e:DataProxyEvent):void {
			ArthropodDebug.log("selectSession e.data " + e.data);
			_dataProxy.selectedSession = e.data as int;
		}
		
		
		
		override public function listNotificationInterests():Array {
			ArthropodDebug.log("listNotificationInterests", ArthropodDebug.PINK);
			return [
				DataProxy.READY,
				AppProxy.APP_STATE_CHANGE
			 ];
		}
		
		override public function handleNotification(notification:INotification):void {
			
			switch ( notification.getName() ) {
				case DataProxy.READY:
					if (_appProxy.appState == AppProxy.CHOSE_SESSION) _dataProxy.getAllSessions();
					break;
					
				case AppProxy.APP_STATE_CHANGE:
					ArthropodDebug.log("ChoseSessionMediator APP_STATE_CHANGE");
					if (notification.getBody() as String == AppProxy.CHOSE_SESSION) view.visible = true;
					else view.visible = false;
					break;
					
				default : break;
			}
				
		}
		
	}
}